/**  
 * @Project: goicter Maven Webapp
 * @Package com.goicter.commons.utils
 * @Title: ResponseUtils.java
 * @Description: Response������
 *
 * @author: Eric Zheng
 * @date 2015-3-15 ����9:14:09
 * @Copyright: Eric Zheng JIMIOPEN.NET. All rights reserved.
 * @version V1.0   
 */
package com.goicter.commons.utils.http;

import java.io.IOException;

import javax.servlet.http.HttpServletResponse;

public class ResponseUtils {
	
	
	/**
	 * ���Json
	 * @param response
	 * @param text
	 */
	public static void renderJson(HttpServletResponse response, String text) {
		render(response, "application/json", text);
	}
	
	/**
	 * ���Json
	 * @param response
	 * @param text
	 * @param callback	�ص�
	 */
	public static void renderJson(HttpServletResponse response, String text,String callback) {
		if(callback==null || "".equals(callback.trim())){
			renderJson(response, text);
		}else{
			renderJson(response, callback + "(" + text + ");");
		}
		
	}
	
	/**
	 * ���text
	 * @param response
	 * @param text
	 */
	public static void renderText(HttpServletResponse response, String text){
		render(response, "text/plain", text);
	}
	
	/**
	 * ���text
	 * @param response
	 * @param text
	 */
	public static void renderXml(HttpServletResponse response, String text){
		render(response, "text/xml", text);
	}
	
	/**
	 * ���html
	 * @param response
	 * @param text
	 */
	public static void renderHtml(HttpServletResponse response, String text){
		render(response, "text/html", text);
	}
	
	
	/**
	 * ���javascript
	 * @param response
	 * @param text
	 */
	public static void renderJavaScript(HttpServletResponse response, String text){
		render(response, "text/javascript", text);
	}
	
	/**
	 * ֱ�����
	 * @param response
	 * @param contentType
	 * @param text
	 */
	public static void render(HttpServletResponse response,String contentType, String text){
		response.setContentType(contentType + ";charset=GBK");
		response.setHeader("Pragma", "No-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expires", 0);
		try {
			response.getWriter().write(text);
		} catch (IOException e) {
		}
	}
	
	/**
	 * ֱ�����
	 * @param response
	 * @param contentType
	 * @param text
	 */
	public static void render(HttpServletResponse response,String contentType, String text,String charset){
		response.setContentType(contentType + ";charset=GBK");
		response.setHeader("Pragma", "No-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expires", 0);
		try {
			response.getWriter().write(text);
		} catch (IOException e) {
		}
	}
}
